package com.kaka.bus.engine.Guide;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.kaka.bus.AppCenter;
import com.kaka.bus.R;
import com.kaka.bus.base.MessageActivity;
import com.kaka.bus.constant.Constant;
import com.kaka.bus.engine.HomeActivity;
import com.kaka.bus.util.StatusBarUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhangqi on 2016/11/14.
 */

public class GuideActivity extends MessageActivity {
    /**
     * splash页面的图的资源ID
     */
    private List<Integer> mIntegers = new ArrayList<>();
    private ViewPager mVpSplash;
    private Button mStart_app;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        mVpSplash = (ViewPager) findViewById(R.id.vp_splash);
        mStart_app = (Button) findViewById(R.id.start_app);

        mIntegers.add(R.mipmap.splash1);
        mIntegers.add(R.mipmap.splash2);
        mIntegers.add(R.mipmap.splash3);
//        mIntegers.add(R.mipmap.splash4);
        mVpSplash.setAdapter(new MySplashAdapter());

        StatusBarUtil.setTranslucentForImageViewInFragment(this,null);

        initListener();
    }

    private void initListener() {
        mVpSplash.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if(position == mIntegers.size() - 1){
                    mStart_app.setVisibility(View.VISIBLE);
                }else{
                    mStart_app.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        mStart_app.setOnClickListener(v -> {
            /**
             * 保存状态
             */
            AppCenter.mSpUtil.putBoolean(Constant.GUIDE,true);
            startActivity(new Intent(this, HomeActivity.class));
            finish();
        });
    }

    class MySplashAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return mIntegers.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView iv = new ImageView(container.getContext());
            iv.setImageResource(mIntegers.get(position));
            iv.setScaleType(ImageView.ScaleType.FIT_XY);
            container.addView(iv);
            return iv;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }
}

